package com.example.trabajofinal.activity;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.trabajofinal.MyApplication;
import com.example.trabajofinal.R;
import com.example.trabajofinal.db.DbHelper;
import com.example.trabajofinal.model.Curso;

public class DetalleActivity extends Activity {

	private TextView tvTitulo;
	private TextView tvDescripcion;
	private TextView tvDiaYHorario;
	private Button btnInscribir;
	private boolean inscripto = false;
	private String mCurso;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detalle_activity);

		tvTitulo = (TextView) findViewById(R.id.tvTitulo);
		tvDescripcion = (TextView) findViewById(R.id.tvDescripcion);
		tvDiaYHorario = (TextView) findViewById(R.id.tvDiaYHorario);
		btnInscribir = (Button) findViewById(R.id.btnInscribir);

		Curso curso = (Curso) getIntent().getSerializableExtra("curso");
		mCurso = curso.getTitulo();
		tvTitulo.setText(curso.getTitulo());
		tvDescripcion.setText(curso.getDescripcion());
		tvDiaYHorario.setText(curso.getDias_horarios());
		
		// cargar imagen del curso
		new DownloadImageTask().execute(curso.getImagen());

		update();
	}


	public void btnInscribir_Click(View v)
	{
		DbHelper db = DbHelper.getDatabaseInstance(this);
		
		if(inscripto){
			db.deleteCurso(MyApplication.CurrentUser, mCurso);
		}
		else{
			db.addCurso(MyApplication.CurrentUser, mCurso);
		}
		
		update();
	}
	
	private void update()
	{
		DbHelper db = DbHelper.getDatabaseInstance(this);
		List<String> cursos = db.getCursos(MyApplication.CurrentUser);
	
		inscripto = cursos.contains(mCurso);
		
		if(inscripto){
			btnInscribir.setText(this.getString(R.string.eliminar_inscripcion));
			btnInscribir.setBackgroundDrawable(getResources().getDrawable(R.drawable.red_button));
		}else{
			btnInscribir.setText(this.getString(R.string.inscribirme));
			btnInscribir.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_button));
		}
	}

	private class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> 
	{   
		protected void onPostExecute(Bitmap result) 
		{
			final ImageView imageView = (ImageView)findViewById(R.id.ivCurso);
			imageView.setImageBitmap(result);
		}

		protected Bitmap doInBackground(String... params) 
		{
			String url = params[0].toString();
			return loadImageFromNetwork(url);
		}
	}
	
	
	private Bitmap loadImageFromNetwork(String url)
	{
		try 
		{
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent()); //"http://flatrateprocessing.com/wp-content/uploads/2012/04/andro.png"
			return bitmap;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}	
}
